/*
 * Copyright (C) 2023 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.templateproject.core.http;

import android.os.Environment;

import com.xuexiang.xutil.tip.ToastUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.UUID;

public class UUIDmake {
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
    public static void saveBitmap() throws IOException {
        // 创建目录
        //获取内部存储状态
        String state = Environment.getExternalStorageState();
        //如果状态不是mounted，无法读写
        if (!state.equals(Environment.MEDIA_MOUNTED)) {
            return;
        }
        String sdCardDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        File appDir = new File(sdCardDir, "CaChe");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = "UUID" + ".txt";//这里是创建一个TXT文件保存我们的UUID
        File file = new File(appDir, fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        //保存android唯一表示符
        try {
            FileWriter fw = new FileWriter(file);
            fw.write(getUUID());
            fw.flush();
            fw.close();
        } catch (FileNotFoundException e) {
            ToastUtils.toast(e.toString());
        } catch (IOException e) {
            ToastUtils.toast(e.toString());
        }
    }
    public static String readKey() throws IOException {
        // 创建目录
        //获取内部存储状态
        String state = Environment.getExternalStorageState();
        //如果状态不是mounted，无法读写
        if (!state.equals(Environment.MEDIA_MOUNTED)) {
            return null;
        }
        String sdCardDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        File appDir = new File(sdCardDir, "CaChe");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = "UUID" + ".txt";//这里是进行读取我们保存文件的名称
        File file = new File(appDir, fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        BufferedReader reader = null;
        StringBuilder content=null;
        try {
            FileReader fr = new FileReader(file);
            content= new StringBuilder();
            reader = new BufferedReader(fr);
            String line;
            while ((line= reader.readLine())!=null){
                content.append(line);
            }
        } catch (Exception e) {
            ToastUtils.toast(e.toString());
        }finally {
            if (reader!=null){
                try {
                    reader.close();
                }catch (IOException e){
                    ToastUtils.toast(e.toString());
                }
            }
        }
        return content.toString();
    }

}
